import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from "@/router/routes";

Vue.use(VueRouter);


const router =new VueRouter({
    mode:'history',
    routes,
    scrollBehavior() {
        // 始终滚动到顶部
        return { x: 0,y:0 }
    },
})

//重写push方法和replace方法
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push(location, onResolve, onReject) {
    // 如果指定了成功或者失败的回调
    if (onResolve || onReject) {
        return originalPush.call(this, location, onResolve, onReject)
    }
    //没有成功或者失败的回调就传空
    return originalPush.call(this, location,()=>{},()=>{})
}

VueRouter.prototype.replace = function (location, onResolve, onReject) {
    if (onResolve || onReject) {
        return originalReplace.call(this, location, onResolve, onReject)
    }
    return originalReplace.call(this, location,()=>{},()=>{})
}

export default router
